package com.apes.fn.server.serviceplanForApp;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.server.servicePlan.model.ServicePlan;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.repository.ServicePlanItemRepository;
import com.apes.fn.server.servicePlan.repository.ServicePlanRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.Arith;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Creator: zouyc
 * CreateDate: 2021-03-26 13:41
 * Description:
 **/
@Service("serviceForQry")
public class ServiceForQry extends ServiceParent{
    public ServiceForQry(ServicePlanRepository param1, ServicePlanItemRepository param2) {
        super(param1, param2);
    }

    /**
     * 需要数据：门店，车牌，金额，状态，创建时间，app单号
     * @param simpleRequest
     * @return
     */
    public Map findForId(SimpleRequest simpleRequest){
        ServicePlan servicelan = servicePlanRepository.findOne(simpleRequest.getJO().getString("id"));
        Dept dept = servicelan.getBelongDept();
        FnCustomerExpand fnCustomerExpand = servicelan.getFnCustomerExpand();
        List<ServicePlanItem> servicePlanItemList = servicelan.getServicePlanItems();
        Double saleAmount = Arith.round(servicePlanItemList.stream().mapToDouble(ServicePlanItem :: SaleAmount).sum(),2);
        return MapUtil.mapper(
                "deptId",dept.getId(),
                "deptName",dept.getName(),
                "carLicense",fnCustomerExpand.getCarLicense(),
                "saleAmount",saleAmount,
                "state",servicelan.getState(),
                "createDate",servicelan.getCreateDate(),
                "sourceType",servicelan.getSourceBusinessType(),
                "source",servicelan.getSourceFrom(),
                "sourceOrderName",servicelan.getSourceBusinessTypeName()
        );
    }
}



